﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLCommuModbus
{
    internal class ConvertData
    {

        public static bool[] UshortToBoolArray(ushort value)
        {
            bool[] result = new bool[16];
            for (int i = 0; i < 16; i++)
            {
                result[i] = (value & (1 << i)) != 0;
            }
            return result;
        }

        public static ushort BoolArrayToUShort(bool[] boolArray)
        {
            ushort ushortValue = 0;
            for (int i = 0; i < 16; i++)
            {
                //bytess[i / 8] |= (byte)(bitss[i] ? (1 << (i % 8)) : 0);
                ushortValue |= (ushort)(boolArray[i] ? (1 << i) : 0);
            }
            return ushortValue;
        }
        public static int UshortsToInt(List<ushort> list, int startIndex)
        {
            byte[] lowOrderValue = BitConverter.GetBytes(list[startIndex]);
            byte[] highOrderValue = BitConverter.GetBytes(list[startIndex + 1]);
            List<byte> buffer = new List<byte>();
            buffer.AddRange(lowOrderValue);
            buffer.AddRange(highOrderValue);
            int value = BitConverter.ToInt32(buffer.ToArray(), 0);
            return value;
        }

        public static double UshortsToDouble(List<ushort> list, int startIndex)
        {
            byte[] AB, CD, EF, GH;
            List<byte> buffer = new List<byte>();
            AB = BitConverter.GetBytes(list[startIndex]);
            CD = BitConverter.GetBytes(list[startIndex + 1]);
            EF = BitConverter.GetBytes(list[startIndex + 2]);
            GH = BitConverter.GetBytes(list[startIndex + 3]);
            buffer.AddRange(AB);
            buffer.AddRange(CD);
            buffer.AddRange(EF);
            buffer.AddRange(GH);
            double value = BitConverter.ToDouble(buffer.ToArray(), 0);
            return value;
        }
        public static float UshortsToFloat(List<ushort> list, int startIndex)
        {
            byte[] lowOrderValue = BitConverter.GetBytes(list[startIndex]);
            byte[] highOrderValue = BitConverter.GetBytes(list[startIndex + 1]);
            List<byte> buffer = new List<byte>();
            buffer.AddRange(highOrderValue);
            buffer.AddRange(lowOrderValue);
            var value = BitConverter.ToSingle(buffer.ToArray(), 0);
            return value;
        }




        public static string UshortsToString(List<ushort> list, int startIndex,int length)
        {
            var bytes = new List<byte>();
            for (int i = startIndex; i < startIndex + length; i++)
            {
                var tmp = BitConverter.GetBytes(list[i]);
                bytes.AddRange(tmp);
            }
            string value = Encoding.UTF8.GetString(bytes.ToArray());
            //去除连接符*&
            return value.Replace("*&", "");
        }

        public static bool UshortsToBool(List<ushort> list, int startIndex)
        {
            bool value = Convert.ToBoolean(list[startIndex]);
            return value;
        }

        public static List<ushort> IntToUshorts(int value)
        {
            List<ushort> list = new List<ushort>();
            byte[] byteInt = BitConverter.GetBytes(value);
            ushort low = BitConverter.ToUInt16(byteInt, 0);
            ushort high = BitConverter.ToUInt16(byteInt, 2);
            list.Add(low);
            list.Add(high);
            return list;
        }
        public static List<ushort> DoubleToUshorts(double value)
        {
            List<ushort> list = new List<ushort>();
            byte[] valueBuffer = BitConverter.GetBytes(value);
            ushort AB = BitConverter.ToUInt16(valueBuffer, 0);
            ushort CD = BitConverter.ToUInt16(valueBuffer, 2);
            ushort EF = BitConverter.ToUInt16(valueBuffer, 4);
            ushort GH = BitConverter.ToUInt16(valueBuffer, 6);
            list.Add(AB);
            list.Add(CD);
            list.Add(EF);
            list.Add(GH);
            return list;
        }
        public static List<ushort> FloatToUshorts(float value)
        {
            List<ushort> list = new List<ushort>();
            byte[] byteInt = BitConverter.GetBytes(value);
            ushort low = BitConverter.ToUInt16(byteInt, 0);
            ushort high = BitConverter.ToUInt16(byteInt, 2);
            list.Add(high);
            list.Add(low);
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxByteLength">字符串（含*&终止符）最大byte长度</param>
        /// <returns></returns>
        public static List<ushort> StringToUshorts(string value, int maxByteLength = 80)
        {
            if (value == null) return null;
            var result = new List<ushort>();

            value = value.ToString() + "*&";
            //string maxByteLength/2 个地址存放数据,   加上结尾符*& 只能存储maxByteLength个byte
            int count = Encoding.UTF8.GetByteCount(value);
            if (count > maxByteLength)
            {
                value = $"Error:more than {maxByteLength} bytes" + "*&";
            }

            //字符串转换成byte数组
            var tmp = Encoding.UTF8.GetBytes(value);
            var lstBytes = new List<byte>();
            lstBytes.AddRange(tmp);
            //不足偶数，补零
            if (tmp.Length % 2 != 0)
            {
                lstBytes.Add(0);
            }
            //byte长度要凑到等于80
            while (lstBytes.Count != maxByteLength)
            {
                lstBytes.Add(0);
            }

            var bytes = lstBytes.ToArray();
            //将byte字节数组转换为ushort集合
            for (int i = 0; i < lstBytes.Count; i += 2)
            {
                result.Add(BitConverter.ToUInt16(bytes, i));
            }
            return result;
        }

        public static ushort BoolToUshort(bool value)
        {
            return Convert.ToUInt16(value); ;
        }

    }
}